import logging
import os
from logging.handlers import RotatingFileHandler
import time

def config_logger(name):
    local_time_tuple = time.localtime()
    formatted_time = time.strftime("%Y-%m-%dT%H-%M-%S", local_time_tuple)

    pwd = os.getcwd()
    filename = pwd +"/"+"log"
    if not os.path.exists(filename):
        os.makedirs(filename)

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    # 避免重复添加handler（防止多次调用时重复打印）
    if logger.handlers:
        return logger


    fh = RotatingFileHandler("./log/test_log{}.txt".format(formatted_time),encoding='utf-8',maxBytes=1024*1024*300,backupCount=10)
    ch = logging.StreamHandler()
    fh.setLevel(logging.INFO)
    ch.setLevel(logging.INFO)
    formatter = logging.Formatter(
        "%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s")

    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger


